package com.chonor.mall.service.impl;

import cn.hutool.core.date.DateUtil;
import com.chonor.mall.dao.OrderDao;
import com.chonor.mall.dao.OrderOperateHistoryDao;
import com.chonor.mall.dto.*;
import com.chonor.mall.enums.GenderEnum;
import com.chonor.mall.enums.OrderStatusEnum;
import com.chonor.mall.mapper.OrderItemMapper;
import com.chonor.mall.mapper.OrderMapper;
import com.chonor.mall.mapper.OrderOperateHistoryMapper;
import com.chonor.mall.model.*;
import com.chonor.mall.service.OrderService;
import com.chonor.mall.util.EnumUtil;
import com.chonor.mall.vo.OrderDetailVO;
import com.chonor.mall.vo.OrderVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;
    
    @Autowired
    private OrderOperateHistoryDao orderOperateHistoryDao;
    
    @Autowired
    private OrderOperateHistoryMapper orderOperateHistoryMapper;
    
    @Override
    public List<Order> list(OrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return orderDao.getList(queryParam);
    }

    @Override
    public List<OrderVO> page(OrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        List<Order> orders = list(queryParam, pageSize, pageNum);
        if(orders.size() < 1) return null;
        List<OrderVO> orderVOS = new ArrayList<>(orders.size());
        for (Order order : orders) {
            OrderVO orderVO = convertOrder(order);
            orderVOS.add(orderVO);
        }
        return orderVOS;
    }

    private OrderVO convertOrder(Order source){
        OrderVO target = new OrderVO();
        BeanUtils.copyProperties(source,target);
        target.setCreateat(DateUtil.formatDate(source.getCreateat()));
        target.setPaymentat(DateUtil.formatDate(source.getPaymentat()));
        target.setDeliveryat(DateUtil.formatDate(source.getDeliveryat()));
        target.setReceiveat(DateUtil.formatDate(source.getReceiveat()));
        target.setModifyat(DateUtil.formatDate(source.getModifyat()));
        target.setStatus(EnumUtil.getMessage(OrderStatusEnum.class, source.getStatus()));
        target.setReceiverAddress(source.getReceiverProvince()+source.getReceiverCity()+source.getReceiverRegion()+source.getReceiverDetailAddress());
        return target;
    }

    @Override
    public int delivery(List<OrderDeliveryParam> deliveryParamList) {
        //批量发货
        int count = orderDao.delivery(deliveryParamList);
        //添加操作记录
        List<OrderOperateHistory> operateHistoryList = deliveryParamList.stream()
                .map(omsOrderDeliveryParam -> {
                    OrderOperateHistory history = new OrderOperateHistory();
                    history.setOrderId(omsOrderDeliveryParam.getId());
                    history.setCreateTime(new Date());
                    history.setOperateMan("后台管理员");
                    history.setOrderStatus(2);
                    history.setNote("完成发货");
                    return history;
                }).collect(Collectors.toList());
        orderOperateHistoryDao.insertList(operateHistoryList);
        return count;
    }

    @Override
    public int deliveryOne(OrderDeliveryParam deliveryParam) {
        Order order = orderMapper.selectByPrimaryKey(deliveryParam.getId());
        order.setDeliveryCompany(deliveryParam.getDeliveryCompany());
        order.setDeliverySn(deliveryParam.getDeliverySn());
        order.setDeliveryat(new Date());
        order.setStatus(2);
        int count = orderMapper.updateByPrimaryKeySelective(order);
        return count;
    }

    @Override
    public int close(List<Long> ids, String note) {
        Order record = new Order();
        record.setStatus(4);
        OrderExample example = new OrderExample();
        example.createCriteria().andIsdeleteEqualTo(0).andIdIn(ids);
        int count = orderMapper.updateByExampleSelective(record, example);
        List<OrderOperateHistory> historyList = ids.stream().map(orderId -> {
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(4);
            history.setNote("订单关闭:"+note);
            return history;
        }).collect(Collectors.toList());
        orderOperateHistoryDao.insertList(historyList);
        return count;
    }

    @Override
    public int delete(List<Long> ids) {
        Order record = new Order();
        record.setIsdelete(1);
        OrderExample example = new OrderExample();
        example.createCriteria().andIsdeleteEqualTo(0).andIdIn(ids);
        return orderMapper.updateByExampleSelective(record, example);
    }

    @Override
    public OrderDetail detail(Long id) {
        return orderDao.getDetail(id);
    }

    @Override
    public OrderDetailVO orderDetail(Long id) {

        OrderDetailVO orderDetailVO = new OrderDetailVO();
        OrderVO itemVO = getItemVO(id);
        BeanUtils.copyProperties(itemVO,orderDetailVO);

        //获取订单行
        OrderItemExample example = new OrderItemExample();
        example.createCriteria().andOrderIdEqualTo(id);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        orderDetailVO.setOrderItemList(orderItems);

        return orderDetailVO;
    }

    @Override
    public Order getItem(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public OrderVO getItemVO(Long id) {
        Order item = getItem(id);
        return convertOrder(item);
    }

    @Override
    public int update(ReceiverInfoParam receiverInfoParam) {
        Order order = new Order();
        order.setId(receiverInfoParam.getId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setDiscountAmount(receiverInfoParam.getDiscountAmount());
        order.setNote(receiverInfoParam.getNote());
        order.setModifyat(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(receiverInfoParam.getId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改订单信息信息");
        orderOperateHistoryMapper.insert(history);
        return count;
    }

    @Override
    public int updateReceiverInfo(ReceiverInfoParam receiverInfoParam) {
        Order order = new Order();
        order.setId(receiverInfoParam.getId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setModifyat(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(receiverInfoParam.getId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        orderOperateHistoryMapper.insert(history);
        return count;
    }

    @Override
    public int updateMoneyInfo(MoneyInfoParam moneyInfoParam) {
        Order order = new Order();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        order.setModifyat(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        orderOperateHistoryMapper.insert(history);
        return count;
    }

    @Override
    public int updateNote(Long id, String note, Integer status) {
        Order order = new Order();
        order.setId(id);
        order.setNote(note);
        order.setModifyat(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(status);
        history.setNote("修改备注信息："+note);
        orderOperateHistoryMapper.insert(history);
        return count;
    }
}
